<?php

/**
 * @version		$Id: view.html.php 10498 2008-07-04 00:05:36Z ian $
 * @package		Joomla
 * @subpackage	Weblinks
 * @copyright	Copyright (C) 2005 - 2008 Open Source Matters. All rights reserved.
 * @license		GNU/GPL, see LICENSE.php
 * Joomla! is free software. This version may have been modified pursuant
 * to the GNU General Public License, and as distributed it includes or
 * is derivative of works licensed under the GNU General Public License or
 * other free or open source software licenses.
 * See COPYRIGHT.php for copyright notices and details.
 */
// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.view');

/**
 * HTML View class for the WebLinks component
 *
 * @static
 * @package		Joomla
 * @subpackage	Weblinks
 * @since 1.0
 */
class SimViewSims extends JView {

    function display($tpl = null) {
        $document = & JFactory::getDocument();
        $document->setTitle('Tìm sim theo yêu cầu');
        $db = & JFactory::getDBO();
        // find sim
        $where = "";
        $where[] = "S.del_flag = 0";
        $minprice = JRequest::getVar("minprice");
        $minprice = ereg_replace("[^0-9]", "", $minprice);
        if($minprice != null &&  is_numeric($minprice)){
            $where[] = "S.price >=" . $minprice;
        }
        $maxprice = JRequest::getVar("maxprice");
        $maxprice = ereg_replace("[^0-9]", "", $maxprice);
        if($maxprice != null &&  is_numeric($maxprice)) $where[] = "S.price <=" . $maxprice;

        $provider = JRequest::getVar("provider");
        $provider = ereg_replace("[^0-9]", "", $provider);
        if ($provider != null &&  is_numeric($provider)) {
            $where[] = "S.provider_id = " . $provider;
        }
        $category = JRequest::getVar("category");
        $category = ereg_replace("[^0-9]", "", $category);
        if ($category != null &&  is_numeric($category)) {
            $where[] = "S.categori_id = " . $category;
        }
        $searchword = JRequest::getVar("searchword");
        if ($searchword != "") {
            $searchword = ereg_replace("[^0-9]", "", $searchword);
//            $searchword = str_replace('.', '', $searchword);
//            $searchword = str_replace(',', '', $searchword);
            if(is_numeric($searchword))
                $where[] = "S.sim_numb_absolute like '%$searchword%'";
        }
        $pref = JRequest::getVar("pref");
        $pref = ereg_replace("[^,0-9]", "", $pref);
        if ($pref != "") {
            $pref = str_replace(' ', '', $pref);
            $pref = str_replace('.', '', $pref);
            $pref = str_replace('_', '', $pref);
            $prefs = explode(",", $pref);
            $where_pref = "";
            foreach ($prefs as $pref) {
                $i++;
                if(is_numeric($pref))
                    $where_pref[] = "S.sim_numb_absolute like '$pref%'";

            }
            $where[] = "(".implode(' OR ', $where_pref).")";
        }
        
        $where = ( $where ? ' WHERE ' . implode(' AND ', $where) : '' );
        // init ordering
        $orderby = " order by S.price desc";

        // get the total number of records
        $query = 'SELECT COUNT(*)'
                . ' FROM #__sim AS S'
                . $where
        ;
        $db->setQuery($query);
        $total = $db->loadResult();
        // pagination
        jimport('joomla.html.pagination');
        $limitstart = JRequest::getVar("limitstart");
        $limit = 50;
        $pagination = new JPagination($total, $limitstart, $limit);
        
        $query = 'SELECT S.*, c.categori_name AS categori_name, pr.provider_name AS provider_name, a.admin_name AS admin_name'
                . ' FROM #__sim AS S'
                . ' INNER JOIN #__sim_categories AS c ON c.categori_id = S.categori_id and c.del_flag = 0'
                . ' INNER JOIN #__sim_provider AS pr ON pr.provider_id = S.provider_id and pr.del_flag = 0'
                . ' INNER JOIN #__sim_admin AS a ON a.admin_id = S.admin_id and a.del_flag = 0'
//                . ' LEFT JOIN #__sim_admin AS a ON a.admin_id = S.admin_id and a.del_flag = 0'
                . $where
                . $orderby
        ;
        $db->setQuery($query, $pagination->limitstart, $pagination->limit);
        $rows = $db->loadObjectList();
        if ($db->getErrorNum()) {
            echo "<div style='display=none'>".$db->stderr()."</div>";
//            return false;
            $this->assignRef('items', null);
            $this->assignRef('total', 0);
        }else{
            $this->assignRef('items', $rows);
            $this->assignRef('pagination', $pagination);
            $this->assignRef('total', $total);
            $this->assignRef('start', $limitstart);
        }
        $this->assignRef('title', JRequest::getVar("title"));
        parent::display($tpl);
    }
}
